package com.zhanqiao.controller;

import com.google.gson.Gson;
import com.zhanqiao.service.OnSaleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/onsale.action")
public class OnSaleController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getParameter("pname"));
        resp.setContentType("application/json;charset=utf8");
        String pname = req.getParameter("pname");
        String pgetprice = req.getParameter("pgetprice");
        String psaleprice = req.getParameter("psaleprice");
        String pduetime = req.getParameter("pduetime");
        String pcount = req.getParameter("pcount");
        String tag = req.getParameter("tag");
        OnSaleService onSaleService = new OnSaleService();
        int i = onSaleService.articleService(pname, Integer.parseInt(pgetprice), Integer.parseInt(psaleprice), pduetime, Integer.parseInt(pcount), tag);
        Map message = new HashMap();
        System.out.println("i=" + i);
        if (i > 0) {
            message.put("msg", "进货成功");
        } else {
            message.put("msg", "进货失败");
        }
        String result = new Gson().toJson(message);
        resp.getWriter().write(result);

    }
}
